package com.imodia.checkoo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.imodia.checkoo.bean.CmsModelBean;
import com.imodia.checkoo.bean.CmsSiteBean;
import com.imodia.checkoo.service.CmsSiteService;


@Controller
@RequestMapping("/hfmanager/site_info/list.do")
public class CmsSiteController {
	private final Log log = LogFactory.getLog(CmsSiteController.class);

	private final String baseUrl = "site_info";

	private final String url_list = "/hfmanager/" + baseUrl + "/" + baseUrl+ "_list";
	private final String url_redirect = "/hfmanager/" + baseUrl+ "/list.do?action=list";
	private final String url_edit = "/hfmanager/" + baseUrl + "/" + baseUrl+ "_edit";	

	
	@Autowired
	private CmsSiteService cmsSiteService;


	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request, "id");
		cmsSiteService.delete(id);
		return new ModelAndView(new RedirectView(request.getContextPath()
				+ url_redirect), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(
			@ModelAttribute("userMessage") CmsSiteBean obj,
			CmsModelBean model, HttpServletRequest request) {
		int x = 0;
		//obj.setRsId(10);
		if( obj.getId()!=null && obj.getId()>0){
			x = cmsSiteService.update(obj);
		}else{
			x = cmsSiteService.insert(obj);
		}
		return new ModelAndView(new RedirectView(request.getContextPath()
				+ url_redirect+"&id="+obj.getId()+"&x="+x), null);
	}

	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request, CmsSiteBean obj) {
		int id = StringUtil.requestInt(request, "id");
		Map<String, Object> m = new HashMap<String, Object>();
		CmsSiteBean info = cmsSiteService.get(id);
		if (info == null)
			info = obj;
		m.put("info", info);
		
		return new ModelAndView(url_edit, m);
	}

	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsSiteBean obj,
			PageBean page) {

		HashMap<String, Object> model = new HashMap<String, Object>();
		SqlParam sql = new SqlParam();
		//sql.andEqualTo("rs_id", "10");
		
		PageResult<CmsSiteBean> pageResult =cmsSiteService.page(sql,page);
		model.put("page", pageResult);
		return new ModelAndView(url_list, model);
	}
	
	
	
}
